import { test as base } from 'appwright';
import { PerformanceTracker } from '../reporters/PerformanceTracker.js';
import TimerHelper from '../utils/TimersHelper.js';

// Create a custom test fixture that handles performance tracking and cleanup
export const test = base.extend({
  // eslint-disable-next-line no-empty-pattern
  performanceTracker: async ({}, use, testInfo) => {
    const performanceTracker = new PerformanceTracker();

    // Provide the tracker to the test
    await use(performanceTracker);

    // After test completes, handle performance metrics and session cleanup
    console.log('🔍 Post-test cleanup: attaching performance metrics...');
    console.log(
      `📊 Found ${performanceTracker.timers.length} timers in tracker`,
    );

    if (performanceTracker.timers.length === 0) {
      console.log('⚠️ No timers found in performance tracker');
    }

    // Always try to attach performance metrics, even if test failed
    try {
      const metrics = await performanceTracker.attachToTest(testInfo);
      console.log(
        `✅ Performance metrics attached: ${
          metrics.steps.length
        } steps, ${metrics.total.toFixed(2)}s total`,
      );
    } catch (error) {
      console.error('❌ Failed to attach performance metrics:', error.message);
    }

    console.log('🔍 Looking for session ID...');

    let sessionId = null;

    if (testInfo && testInfo.annotations) {
      sessionId = testInfo.annotations.find(
        (annotation) => annotation.type === 'sessionId',
      )?.description;
    }

    if (sessionId) {
      // Store session data as a test attachment for the reporter to find
      await testInfo.attach('session-data', {
        body: JSON.stringify({
          sessionId,
          testTitle: testInfo.title,
          projectName: testInfo.project.name,
          timestamp: new Date().toISOString(),
        }),
        contentType: 'application/json',
      });

      await performanceTracker.storeSessionData(sessionId, testInfo.title);
      console.log(`✅ Session data stored: ${sessionId}`);
    } else {
      console.log('⚠️ No session ID found - video URL cannot be retrieved');
    }
  },
});

export { expect } from 'appwright';
